<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Calendar model to make it easier to use the calendar
 *
 * @category    RedCross
 * @package     Core_Model
 */
class Core_Model_Calendar
{

	private $_passiveMessages;
	private $_shifts;
	private $_date;
	private $_dayCounter;

	public function __construct(RedCross_Date $date)
	{
		$this->_dayCounter = 0;
		$this->_date = $date;
		//TODO: set $this->_date to the 1st of the month in the provided month
	}

	public function setPassiveMessages($messages) //array of Core_Model_PassiveMessage or something
	{
		//TODO: validate
		$this->_passiveMessages = array();
		for ($i = 0; $i <= $this->getDaysInMonth(); $i++)
		{
			$this->_passiveMessages[] = false;
		}
		if (count($messages) > 0)
		{
			foreach ($messages as $message)
			{
				$date = new RedCross_Date($message->date);
				$this->_passiveMessages[(int)$date->get(Zend_Date::DAY)] = $message;
			}
		}
	}

	public function setShifts($shifts)
	{
        //TODO: validate
		$this->_shifts = array();
		for ($i = 0; $i <= $this->getDaysInMonth(); $i++)
		{
			$this->_shifts[] = array(false);
		}
		if (count($shifts) > 0)
		{
			foreach ($shifts as $shift)
			{
				$date = new RedCross_Date($shift->appearance_time);
				$to_date = new RedCross_Date($shift->to_date);
				while($to_date->compare($date) != -1){
					$index = (int)$date->get(Zend_Date::DAY);
					if ($this->_shifts[$index][0] == false)
					{
						$this->_shifts[$index] = array($shift);
					} else
					{
						$this->_shifts[$index][] = $shift;
					}
					$date->add(1,Zend_Date::DAY);
				}//end while
			}
		}
	}

	public function hasPassivemessageOnDate($day)
	{
		return getPassiveMessageOnDay($day) != false;
	}

	public function getPassiveMessageOnDay($day = null)
	{
		if (count($this->_passiveMessages) == 0) return false;
		if (is_null($day)) $day = $this->getCurrentDayNumber();
		if (array_key_exists($day,$this->_passiveMessages))
		{
			return $this->_passiveMessages[$day];
		}
		return false;
	}

	public function getShiftsOnDay($day = null)
	{
		if (count($this->_shifts) == 0) return false;
		if (is_null($day)) $day = $this->getCurrentDayNumber();
		if (array_key_exists($day,$this->_shifts))
		{
			return $this->_shifts[$day];
		}
		return false;
	}

	public function getNumberOfDaysInFirstWeekNotInThisMonth()
	{
		$firstWeekDayOfMonth = $this->_date->get(Zend_Date::WEEKDAY_8601);
		return $firstWeekDayOfMonth - 1;
	}

	public function getDaysInMonth()
	{
		return $this->_date->get(Zend_Date::MONTH_DAYS);
	}

	public function dayPassed()
	{
		$this->_dayCounter++;
		
	}

	public function hasMoreDays()
	{
		return $this->_dayCounter < $this->getDaysInMonth();
	}

	public function isNewWeek()
	{
		return (($this->getNumberOfDaysInFirstWeekNotInThisMonth() + $this->_dayCounter) % 7) == 0;
	}

	public function getCurrentDayNumber()
	{
		return $this->_dayCounter + 1;
	}
	public function getDate()
	{
		return $this->_date;
	}
	
	public function getCurrentDate()
	{
		$tempDate = new RedCross_Date($this->_date);
		$tempDate->add($this->_dayCounter, Zend_Date::DAY);
		
		return $tempDate;
	}

}
